import { ref, onMounted, toRefs, reactive, computed, watch,} from 'vue'
import getTableData from '/@/utils/getTableData'

// 深拷贝
const deepCopy = (data: any) => {
    let res: any = typeof data.splice === 'function' ? [] : {}
    if (typeof data === 'object') {
        for (let key in data) {
            if (data[key] && typeof data[key] === 'object') {
                res[key] = deepCopy(data[key])
            } else {
                res[key] = data[key]
            }
        }
    }
    return res
}

const initSelectList = (fetchMethod: Function, otherPrams: object = {}, searchObj: any = reactive({}), multiple: boolean = true, marker: string = 'id', fetchInlife: boolean = false ) => {

    const tableObj = getTableData(fetchMethod, otherPrams, searchObj, fetchInlife)
    
    // state数据
    const state =  reactive({
        listData: [] as any[],
        selectedRows: [] as any[],
        selectAll: false,
    })

    // 设置回显状态
    const setSelectedStatus = () => {
        // 将原有选中的数据设置为选中状态
        if (state.selectedRows.length) {
            let selectedNumber = 0
            state.listData.forEach((item, index) => {
                let isExisted = state.selectedRows.some( idItem => idItem[marker] == item[marker] )
                if (isExisted) {
                    item.checked = true
                    selectedNumber++
                } else {
                    item.checked = false
                }
            })
            state.selectAll = selectedNumber == state.selectedRows.length? true : false
        }
    }

    // 选中事件
    const checkboxHandle = (val: any, data: any) => {
        data.checked = val
        if (multiple) {
            // 先判断已选中是否已有该数据
            let isExisted = state.selectedRows.some( (rowItem: any)=> rowItem[marker]== data[marker])
            if (isExisted) {
                if (!val) { //存在 并且取消，就删除
                    for( let i = 0; i < state.selectedRows.length; i++) {
                        let row = state.selectedRows[i]
                        if (row[marker] == data[marker]) {
                            state.selectedRows.splice(i, 1)
                            break;
                        }
                    }
                }
            } else {
                if (val) {
                    state.selectedRows.push(data)
                }
            }
        } else { //单选
            if (val == true) {
                state.selectedRows = []
                state.listData.forEach( (item: any) => {
                    if (item[marker] != data[marker]) {
                        item.checked = false
                    }
                })
                state.selectedRows = []
                state.selectedRows.push(data)

            }
        }
        if (state.listData.length == state.selectedRows.length) {
            state.selectAll = true
        } else {
            state.selectAll = false
        }
    }

    watch( ()=> tableObj.list.value, () => {
        // console.log(888)
        let arr: any = deepCopy(tableObj.list.value)
        state.listData = arr.map ( (v: any) => {
            return {
                ...v,
                checked: false,
            }
        })
        setSelectedStatus()
    })

    // 计算选中的中间态
    const getIndeterminate = computed( () => {
        let res = false
        if (state.selectedRows.length) {
            let totalNumber = 0;
            for (let i = 0; i < state.listData.length; i++) {
                let item = state.listData[i]
                let isExisted = state.selectedRows.some( (v: any) => {
                    return item[marker] == v[marker]
                })
                if (isExisted) {
                    totalNumber++
                }
            }
            if (totalNumber == state.listData.length ) {
                res = false
                state.selectAll = true
            } else if (totalNumber == 0) {
                state.selectAll = false
            } else {
                res = true
            }
        }
        return res
    })

    // 全选
    const selectAllChange = (check: boolean) => {
        if (check) {
            state.listData.forEach( (v: any) => {
                v.checked = true
                let isExisted: boolean = state.selectedRows.some( (item: any) => {
                    return item[marker] == v[marker]
                })
                if (!isExisted) {
                    state.selectedRows.push(v)
                }
            })
        } else {
            state.listData.forEach( (v: any) => {
                v.checked = false
                for( let i = 0; i < state.selectedRows.length; i++) {
                    let item = state.selectedRows[i]
                    if (item[marker] == v[marker]) {
                        state.selectedRows.splice(i, 1)
                        break;
                    }
                }
            })
        }
    }

    return {
        ...toRefs(state),
        checkboxHandle,
        getIndeterminate,
        selectAllChange,
        setSelectedStatus,
        ...tableObj,
    }

}

export default initSelectList